'use strict'
// const moment = require('moment')

module.exports = (sequelize, DataTypes) => {
  const Article = sequelize.define(
    'article',
    {
      title: {
        type: DataTypes.STRING(50),
        allowNull: false,
        validate: {
          isEven (value) {
            if (!value) throw new Error('该字段不能为空')
            if (value.lenth > 50) throw new Error('该字段不能超过50字符')
          }
        }
      },
      desc: {
        type: DataTypes.STRING(50),
        allowNull: false,
        defaultValue: '',
        validate: {
          isEven (value) {
            if (value.length > 50) throw new Error('该字段不能超过50字符')
          }
        }
      },
      content: {
        type: DataTypes.TEXT('long'),
        allowNull: false,
        validate: {
          isEven (value) {
            if (!value) throw new Error('该字段不能为空')
          }
        }
      }
    }, {
      timestamps: true
    }
  )
  Article.getCount = async (option = {}) => {
    const count = await Article.count(option)
    return count
  }
  return Article
}
